package com.yicong.ants.scenic

import android.app.Activity
import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.Typeface
import android.graphics.drawable.Drawable
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.core.widget.ContentLoadingProgressBar
import androidx.core.widget.NestedScrollView
import androidx.databinding.BindingAdapter
import com.blankj.utilcode.util.BarUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import com.yicong.ants.R

@BindingAdapter("url", "placeholder", requireAll = false)
fun setUrl(
    imageView: ImageView,
    url: String?,
    placeholder: Int?
) {
    imageView.setTag(R.id.tag_url, url);
    Glide.with(imageView)
        .asBitmap()
        .load(url)
        .placeholder(placeholder ?: 0)
        .into(object : CustomTarget<Bitmap>() {
            override fun onLoadStarted(placeholder: Drawable?) {
                imageView.setImageDrawable(placeholder)
            }

            override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                val localUrl = imageView.getTag(R.id.tag_url)
                if (localUrl != url) return
                imageView.setImageBitmap(resource)
            }

            override fun onLoadCleared(placeholder: Drawable?) {
                imageView.setImageDrawable(placeholder)
            }

            override fun onLoadFailed(errorDrawable: Drawable?) {
                imageView.setImageDrawable(errorDrawable)
            }
        })
}

@BindingAdapter("is_enable")
fun setViewEnable(view: View, isEnable: Boolean) {
    view.isEnabled = isEnable
}

@BindingAdapter("maskPhoneNumber")
fun maskPhoneNumber(textView: TextView, phoneNumber: String?) {
    if (phoneNumber.isNullOrBlank()) return
    try {
        textView.text = phoneNumber.mapIndexed { index, c ->
            if (index in 3..6) '*' else c
        }.joinToString("")
    } catch (_: Exception) {
    }
}

@BindingAdapter(
    "alphaWhenScrollId",
    "alphaWhenScrollRange",
    "alphaWhenStatusBarLight",
    requireAll = false
)
fun setAlphaWhenScroll(view: View, id: Int, range: Float, alphaWhenStatusBarLight: Boolean?) {
    val scrollView = (view.parent as ViewGroup).findViewById<NestedScrollView>(id)
    scrollView.setOnScrollChangeListener(NestedScrollView.OnScrollChangeListener { _, _, scrollY, _, _ ->
        var aplha = 1F * scrollY.toFloat() / range
        if (aplha > 1) {
            aplha = 1F
        }
        view.alpha = aplha
        (view.context as Activity).window?.let {
            if (alphaWhenStatusBarLight == null) return@OnScrollChangeListener
            val isLightMode = alphaWhenStatusBarLight == true
            if (view.alpha > 0.5) {
                BarUtils.setStatusBarLightMode(it, isLightMode)
            } else {
                BarUtils.setStatusBarLightMode(it, !isLightMode)
            }
        }
    })
}

@BindingAdapter(
    "shadowId",
    "titleId",
    "alphaWhenScrollId",
    "alphaWhenScrollRange",
    "alphaWhenStatusBarLight",
    requireAll = false
)
fun setAlphaWhenScroll(
    view: View,
    shadow: Int,
    titleId : Int,
    id: Int,
    range: Float,
    alphaWhenStatusBarLight: Boolean?
) {
    val scrollView = (view.parent as ViewGroup).findViewById<NestedScrollView>(id)
    val titleView = (view.parent as ViewGroup).findViewById<View>(titleId)
    val shadowView = (view.parent as ViewGroup).findViewById<View>(shadow)
    scrollView.setOnScrollChangeListener(NestedScrollView.OnScrollChangeListener { _, _, scrollY, _, _ ->
        var aplha = 1F * scrollY.toFloat() / range
        if (aplha > 1) {
            aplha = 1F
        }
        shadowView.isVisible = aplha == 1f
        titleView.alpha = aplha
        (titleView.context as Activity).window?.let {
            if (alphaWhenStatusBarLight == null) return@OnScrollChangeListener
            val isLightMode = alphaWhenStatusBarLight == true
            if (titleView.alpha > 0.5) {
                BarUtils.setStatusBarLightMode(it, isLightMode)
            } else {
                BarUtils.setStatusBarLightMode(it, !isLightMode)
            }
        }
    })
}

@BindingAdapter("isBold")
fun setBold(view: TextView, isBold: Boolean) {
    if (isBold) {
        view.setTypeface(null, Typeface.BOLD)
    } else {
        view.setTypeface(null, Typeface.NORMAL)
    }
}

@BindingAdapter("isHidden")
fun setHidden(view: ContentLoadingProgressBar, isHidden: Boolean) {
    if (isHidden) {
        view.hide()
    } else {
        view.show()
    }
}

@BindingAdapter("transparentStatusBar")
fun transparentStatusBar(view: ViewGroup, transparent: Boolean) {
    (view.context as Activity).let {
        if (transparent) {
            BarUtils.transparentStatusBar(it)
        } else {
            BarUtils.setStatusBarColor(it, Color.WHITE)
        }
    }
}

@BindingAdapter("transparentNavBar")
fun transparentNavBar(view: ViewGroup, transparent: Boolean) {
    (view.context as Activity).let {
        if (transparent) {
            BarUtils.setNavBarColor(it, Color.TRANSPARENT)
        } else {
            BarUtils.setNavBarColor(it, Color.WHITE)
        }
    }
}

@BindingAdapter("statusBarDarkText")
fun setStatusBarLightMode(view: ViewGroup, darkText: Boolean) {
    (view.context as Activity).let {
        BarUtils.setStatusBarLightMode(it, darkText)
    }
}

@BindingAdapter("marginTop")
fun setMarginTop(view: View, top: Int) {
    if (view.layoutParams is ViewGroup.MarginLayoutParams) {
        (view.layoutParams as ViewGroup.MarginLayoutParams).topMargin = top
    }
}


